package com.metaera.app.controller.presenter;

import com.metaera.app.controller.core.SimplePaginationPresenter;
import com.metaera.app.data.DataRepository;
import com.metaera.app.data.model.ArticleListResponse;
import com.metaera.app.data.model.BasicListResponse;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;

/**
 * 快讯
 */
public class FlashNewsListPresenter extends SimplePaginationPresenter<ArticleListResponse> {

    private final int mCategoryID;

    private int currentTimestamp;

    public FlashNewsListPresenter(int categoryID) {
        mCategoryID = categoryID;
    }

    @Override
    protected Observable<BasicListResponse<ArticleListResponse>> execute() {
        return DataRepository.getInstance()
                .getArticleList(
                        mCategoryID,
                        ApiParam.GET_ARTICLE_LIST_FLASH_NEWS_TYPE,
                        mPage,
                        ApiParam.PAGE_SIZE
                )
                .map(response -> {

                    BasicListResponse.PageDataEntity<ArticleListResponse> data = response.getData();
                    if (data == null) {
                        return response;
                    }

                    List<ArticleListResponse> list = data.getList();
                    if (list == null) {
                        return response;
                    }

                    List<ArticleListResponse> newList = new ArrayList<>();

                    for (int i = 0; i < list.size(); i++) {
                        ArticleListResponse item = list.get(i);

//                        if ((i + 1) < list.size()){
//                            if (!DateUtils.isSameDate(item.getRelease_time_stamp(), list.get(i + 1).getRelease_time_stamp())) {
//                                ArticleListResponse e = new ArticleListResponse();
//                                e.setItemType(ArticleListResponse.DATE_TYPE);
//                                e.setRelease_time_stamp(item.getRelease_time_stamp());
//                                newList.add(e);
//                            }
//                        }

                        newList.add(item);
                    }

//                    for (ArticleListResponse item : list) {
//                        int nextTimestamp = item.getRelease_time_stamp();
//
//                        // 添加日期类型
//                        if (!DateUtils.isSameDate(currentTimestamp, nextTimestamp)) {
//                            currentTimestamp = nextTimestamp;
//                            ArticleListResponse e = new ArticleListResponse();
//                            e.setItemType(ArticleListResponse.DATE_TYPE);
//                            e.setRelease_time_stamp(nextTimestamp);
//                            newList.add(e);
//                        }
//
//                        // 添加内容条目类型
//                        newList.add(item);
//                    }

                    data.setItems(newList);

                    return response;
                });
    }

}
